package cn.sourcespro.controller;

import cn.sourcespro.data.dto.SearchUserDto;
import cn.sourcespro.data.entity.User;
import cn.sourcespro.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * 用户 Controller
 *
 * @author zhanghw
 * @date 2018/10/15
 */
@Controller
@RequestMapping(value = "admin/user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService service;

    @GetMapping("list")
    String list(SearchUserDto condition, Model model){
        IPage<User> page = service.findAll(condition);
        model.addAttribute("page", page);
        return "/admin/user/list";
    }

    @GetMapping("v_add")
    String add(){
        return "/admin/user/add";
    }

    @PostMapping("add")
    String add(User bean){
        User repo = service.add(bean);
        return "redirect:/admin/user/list";
    }

    @GetMapping("v_update")
    String update(Integer id, Model model){
        model.addAttribute("item", service.findById(id));
        return "/admin/user/edit";
    }

    @PostMapping("update")
    String update(User bean){
        User repo = service.update(bean);
        return "redirect:/admin/user/list";
    }

    @GetMapping("delete")
    String delete(String id, int page){
        service.delete(id);
        return "redirect:/admin/user/list?page="+page;
    }

    @GetMapping("batch_delete")
    String delete(String[] ids){
        for (String id : ids) {
            service.delete(id);
        }
        return "redirect:/admin/user/list";
    }
}